/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-11 10:15:43
 */
import { reactive, markRaw } from 'vue';
import { removeUndefinedParams } from '@/utils/index';
import { BasicPagingListParams } from '@/apis/basicModel';
import { getReleaseList } from '@/apis/system/information';
import { GetReleaseListParams } from '@/apis/system/model/informationModel';
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable';
import { ColumnSettingTypeEnum } from '@/components/CPageTable';
import { CTableProps } from '@/components/CTable';
import { moduleTypeEnum2 } from '@/constants/system/systemEnum';
import { useRoute } from 'vue-router';

export const useInfoList = () => {
  const route = useRoute();
  const { name } = route;
  // 搜索字段及其他
  const searchForm = reactive<GetReleaseListParams>({
    title: '',
    releaseStatus: '',
    sendType: ''
  });

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '标题',
      placeholder: '标题',
      field: 'title',
      type: 'input'
    },
    {
      name: '发布状态',
      placeholder: '发布状态',
      field: 'releaseStatus',
      type: 'select',
      fixedOption: 'releaseStatusTypeOption',
      style: 'width:134px'
    },
    {
      name: '发布类型',
      placeholder: '发布类型',
      field: 'sendType',
      type: 'select',
      fixedOption: 'releaseTypeOption'
    }
  ]);

  // 表格配置
  const tableOptions: CTableProps = {
    selection: false,
    loadData
  };

  function loadData(parameter: BasicPagingListParams) {
    const params: GetReleaseListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    );
    return getReleaseList(params);
  }

  // 处理参数
  const handleParams = (): GetReleaseListParams => {
    // 参数
    let params: GetReleaseListParams = {
      module: moduleTypeEnum2[name as keyof typeof moduleTypeEnum2],
      title: searchForm.title,
      releaseStatus: searchForm.releaseStatus,
      sendType: searchForm.sendType
    };
    params = removeUndefinedParams(params);
    return params;
  };
  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '标题',
      field: 'subtitle',
      minWidth: 150,
      slot: 'title',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '发布类型',
      field: 'sendType',
      width: 120,
      slot: 'sendType',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '副标题',
      field: 'subtitle',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '附件',
      field: 'file',
      width: 60,
      slot: 'fileSlot',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人',
      field: 'releasePeopleName',
      width: 120,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建时间',
      field: 'createTime',
      width: 180,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '发布状态',
      field: 'status',
      slot: 'status',
      width: 100,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '发布时间',
      field: 'releaseTime',
      width: 180,
      slot: 'releaseTime',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 160,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ];

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting,
    title: route.meta.title,
    module: moduleTypeEnum2[name as keyof typeof moduleTypeEnum2]
  };
};
